package org.dawing.shop.cart.query.handler;

import org.axonframework.eventhandling.EventHandler;
import org.dawing.shop.cart.common.dto.ProductDto;
import org.dawing.shop.cart.common.dto.ShopDto;
import org.dawing.shop.cart.query.entry.CartEntry;
import org.dawing.shop.cart.query.repository.CartEntryRepository;
import org.dawning.shop.common.events.cart.CartAddEvent;
import org.dawning.shop.common.events.cart.CartCreateEvent;
import org.dawning.shop.common.events.cart.ProductQuantityChangeEvent;
import org.dawning.shop.common.utils.BeanMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by miou on 2017/6/14 0014.
 */
@Component
public class CartEntryHandler {

    @Autowired
    private CartEntryRepository repository;

    @EventHandler
    public void on(CartCreateEvent event){
        CartEntry cartEntry = new CartEntry();
        cartEntry.setId(event.getCartId().getIdentifier());
        Map<String,ShopDto> shopDtoMap = new HashMap<>();
        Map<String,ProductDto> productDtoMap = new HashMap<>();
        ShopDto shopDto = BeanMapper.map(event.getCartProduct(),ShopDto.class);
        shopDto.setId(event.getCartProduct().getShopId());
        ProductDto productDto = BeanMapper.map(event.getCartProduct(),ProductDto.class);
        productDtoMap.put(event.getCartProduct().getShopId()+event.getSku(),productDto);
        shopDto.setProducts(productDtoMap);
        shopDtoMap.put(shopDto.getId(),shopDto);
        cartEntry.setShops(shopDtoMap);
        repository.save(cartEntry);
    }

    @EventHandler
    public void on(CartAddEvent event){
        CartEntry cartEntry = repository.findOne(event.getCartId().getIdentifier());
        if(null!=cartEntry.getShops().get(event.getCartProduct().getShopId())){
            ProductDto productDto = BeanMapper.map(event.getCartProduct(),ProductDto.class);
            cartEntry.getShops().get(event.getCartProduct().getShopId()).getProducts().
                    put(event.getCartProduct().getShopId()+event.getSku(),productDto);
        }else{
            ShopDto shopDto = BeanMapper.map(event.getCartProduct(),ShopDto.class);
            shopDto.setId(event.getCartProduct().getShopId());
            Map<String,ProductDto> productDtoMap = new HashMap<>();
            ProductDto productDto = BeanMapper.map(event.getCartProduct(),ProductDto.class);
            productDtoMap.put(event.getCartProduct().getShopId()+event.getSku(),productDto);
            shopDto.setProducts(productDtoMap);
            cartEntry.getShops().put(shopDto.getId(),shopDto);
        }
        repository.save(cartEntry);
    }

    @EventHandler
    public void on(ProductQuantityChangeEvent event) {
        CartEntry cartEntry = repository.findOne(event.getCartId().getIdentifier());
        ProductDto productDto = cartEntry.getShops().get(event.getShopId()).getProducts().get(event.getShopId()+event.getSku());
        productDto.setQuantity(event.getQuantity());
        cartEntry.getShops().get(event.getShopId()).getProducts().put(event.getShopId()+event.getSku(), productDto);
    }
}
